#!/usr/bin/env python3
import thriftpy
import jpype
from thriftpy.rpc import make_client
from os.path import abspath, dirname

class score_filter:

    def __init__(self,ip_adrrs):
        #初始化
        __currdir = dirname(abspath(__file__))
        config_path = __currdir+"/classifier.thrift"
        #config_path = "H:/研究课题/优亿科技/通用网络爬虫系统/爬虫设计材料/anthelion-master/代码/classifier.thrift"
        classifier = thriftpy.load(config_path, module_name="classifier_thrift")
        self.classifier_client = make_client(classifier.Classifier, ip_adrrs, 8090)

    def get_containsSem(self, result):
        if "parsed-data" in result.keys() or "outlink" in result.keys():
            containsSem = True
        else:
            containsSem = False
        return containsSem

    def updateModel(self, url, meta_data):
        '''url is the current url,
        meta_data is current url's stored information, it stored the containsSem, containsSemFather and containsSemFatherforSub
        '''
        if meta_data["score_meta_data"]:
            meta_data["score_meta_data"] = {"containsSem":False,"containsSemFather":False}
        #是否包含语义，父类是否包含语义
        containsSem = meta_data["score_meta_data"]["containsSem"]
        semFather = meta_data["score_meta_data"]["containsSemFather"]
        #更新模型
        self.classifier_client.updateModel(url, containsSem, semFather)
        meta_data["score_meta_data"]["containsSemFatherforSub"] = containsSem
        return meta_data

    def get_outlinks_score(self, outlinks, meta_data):
        '''meta_data is the current url information ,
         outlinks is the set of outlink which current url parsed, it stored in the parserModel result.'''
        if "containsSemFatherforSub" not in meta_data["score_meta_data"].keys():
            semFather = False
        else:
            semFather = meta_data["score_meta_data"]["containsSemFatherforSub"]
        meta_data["score_meta_data"]["containsSemFather"] = semFather
        scores = self.classifier_client.getScores(semFather, outlinks.keys())
        outlinks_scores = dict(zip(outlinks, scores))
        for url in outlinks.keys():
            outlinks[url]["score"] = outlinks_scores[url]
        return outlinks, meta_data

if __name__ == "__main__":
    import random
    from pyspider.libs.response import Response
    r = Response()
    filter = score_filter('127.0.0.1')
    for i in range(106):
        random_str = ''.join(random.sample('zyxwvutsrqponmlkjihgfedcba',i%7))
        r.url = "http://www.baidu.com/%s"%(random_str)
        if not r.save:
            r.save = {"score_meta_data":{"containsSem":random.choice([True,False]),"containsSemFather":random.choice([True,False])}}
        r.save = filter.updateModel(r.url, r.save)
        outlinks = {"http://www.baidu.com/a/5/%s"%random_str:{},
                "http://www.baidu.com/asdgvbs":{},}
        score,r.save = filter.get_outlinks_score(outlinks, r.save)
        print(score)
